package xin.alum.aim.redis;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.protobuf.GeneratedMessageV3;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import xin.alum.aim.constant.AIMConstant;
import xin.alum.aim.constant.Constants;
import xin.alum.aim.model.Pusher;
import xin.alum.aim.util.ProtoDeserializer;
import xin.alum.aim.util.ProtoSerializer;

@AutoConfigureBefore(RedisAutoConfiguration.class)
@ConditionalOnProperty(prefix = AIMConstant.CLUSTER_PROPERTIES_PREFIX, name = AIMConstant.CLUSTER_PROPERTIES_MODE, havingValue = "redis")
public class RedisConfig {
    @Bean
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();

        //将proto序列化反序列化加入到Mapper里
        SimpleModule module = new SimpleModule();
        module.addDeserializer(GeneratedMessageV3.class, new ProtoDeserializer());
        module.addSerializer(GeneratedMessageV3.class, new ProtoSerializer());
        om.registerModule(module);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        om.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        return jackson2JsonRedisSerializer;
    }

    @Bean
    RedisTemplate<String,Object> aimRedisTemplate(RedisConnectionFactory factory,Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }


    @Bean
    public KickMessageListener kickMessageListener() {
        return new KickMessageListener();
    }

    @Bean
    public PushMessageListener pushMessageListener() {
        return new PushMessageListener();
    }

    @Bean
    public ReplyMessageListener replyMessageListener() {
        return new ReplyMessageListener();
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,
                                                                       PushMessageListener pushMessageListener,
                                                                       KickMessageListener kickMessageListener,
                                                                       ReplyMessageListener replyMessageListener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        if (!connectionFactory.getConnection().isClosed()) {
            container.setConnectionFactory(connectionFactory);
            container.addMessageListener(pushMessageListener, new ChannelTopic(Constants.PUSH_MESSAGE_INNER_QUEUE));
            container.addMessageListener(kickMessageListener, new ChannelTopic(Constants.BIND_MESSAGE_INNER_QUEUE));
            container.addMessageListener(replyMessageListener, new ChannelTopic(Constants.REPLY_MESSAGE_INNER_QUEUE));
        }
        return container;
    }

    @Bean
    public RedisPusher redisPusher() {
        return new RedisPusher();
    }

}